<?php

###########################################################################
#   Copyright (C) 2012 CDNPAL LTD by Christopher Gu                       #
#   resources@cdnpal.com                                                  #
#                                                                         #
#   coded for Php 4.3/5.X and above                                       #
#   and AllowOverride all for .htaccess                                   #
#                                                                         #
#   This program is free software released                                #
#   under the GPL version 2 license:                                      #
#   http://www.gnu.org/licenses/gpl-2.0.htm                               #
#                                                                         #
#   This program is distributed WITHOUT ANY WARRANTY                      #
#   or FITNESS FOR A PARTICULAR PURPOSE.                                  #
#                                                                         #
#   DADF-SDK                                                              #
#                                                                         #
###########################################################################

abstract class GenericDataFactory extends Zend_Db_Table_Abstract
{
    protected $_primary;
	protected $_sequence = true;
	protected $_rows = null;
	protected $_name;
    
	public static function get_instance($child_class, $instance) {
		if ($instance == null):
			$instance = new $child_class();
		endif;
		return $instance;
	}
	
	
    function __construct() {
	    parent::__construct();
    }
    
    public function get_row($params = null) {

		$select = $this->select();
		foreach ($params as $name => $value):
			$select->where($name . ' = ?', $value);
		endforeach;
		
		$select->limit(1, 0);
		
		//var_dump($select); exit;
		
		//$select->order('UpdateTime');
	    $row = $this->fetchRow($select);
	    if ($row):
	    	return new $this->_name($row);
	    else:
	    	return null;
	    endif;
    }
    
    public function get($params = null) {
    	
		$select = $this->select();
		foreach ($params as $name => $value):
			$select->where($name . ' = ?', $value);
		endforeach;
		// $select->limit(10, 0);
		//$select->order('UpdateTime');
	    $this->_rows = $this->fetchAll($select);
	    
	    $items = array();
	    foreach ($this->_rows as $_row):
	    	$items[] = new $this->_name($_row);
	    endforeach;
	    
	    return $items;
    }
    
}